package com.hxzy.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.Nationality;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.utils.StringUtils;
import com.hxzy.system.domain.dto.NationalitySearchDTO;
import com.hxzy.system.service.NationalityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@Api(tags = "国籍接口")
@RequestMapping(value = "api/tzxm/nationality")
public class NationalityController extends BaseController {

    @Autowired
    private NationalityService nationalityService;

    @ApiOperation(value = "分页查询所有数据")
    @GetMapping(value = "/selectList")
    public R listAll (NationalitySearchDTO nationalitySearchDTO) {
        IPage<Nationality> page = this.nationalityService.selectListAll(nationalitySearchDTO);
        return pageVO(page);
    }

    @GetMapping(value = "/{dictSort}")
    @ApiOperation(value = "根据id查询国籍信息")
    public R selectById (@PathVariable Long dictSort) {
        if (StringUtils.isNull(dictSort)) {
            return R.build(AckCode.USER_PARAM_IS_NOT_NULL);
        }
        Nationality nationality = this.nationalityService.getById(dictSort);
        if (Objects.isNull(nationality)) {
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(nationality);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增国籍信息")
    public R addNationality (@RequestBody @Validated Nationality nationality) {
        if (Objects.isNull(nationality)) {
            R.build(AckCode.USER_PARAM_IS_NOT_NULL);
        }
        boolean boo = this.nationalityService.addNationality(nationality);
        return boo ? R.ok() : R.build(AckCode.FAIL);
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "根据id修改信息")
    public R updateById (@RequestBody @Validated Nationality nationality) {
        boolean b = this.nationalityService.updateById(nationality);
        return b ? R.ok() : R.build(AckCode.FAIL);
    }

    @PostMapping(value = "/delete/{id}")
    @ApiOperation(value = "根据id删除")
    public R deleteById (@RequestBody @Validated @PathVariable Long id) {
        Nationality nationality = this.nationalityService.getById(id);
        if (Objects.isNull(nationality)) {
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        nationality.setStatus("1");
        this.nationalityService.updateById(nationality);
        return R.ok();
    }
}
